package com.github.aloxc.plugin.restplus.listener;

import com.github.aloxc.plugin.restplus.common.Consts;
import com.github.aloxc.plugin.restplus.component.SettingForm;
import com.github.aloxc.plugin.restplus.entity.SettingFormType;
import com.github.aloxc.plugin.restplus.entity.SettingListenerContext;
import com.github.aloxc.plugin.restplus.i18n.I18Bundle;
import com.github.aloxc.plugin.restplus.utils.StringUtils;
import com.github.aloxc.plugin.restplus.entity.EnvironmentConfig;
import com.github.aloxc.plugin.restplus.entity.RestConfig;
import com.github.aloxc.plugin.restplus.navigator.RestServicesNavigator;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.IconLoader;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
 *
 * @author leero
 */
public class ChangeEnvironmentListener extends MouseAdapter {
    private final static String SPLIT = "   ";
    private Project project;
    public ChangeEnvironmentListener(Project project) {
        this.project = project;
    }

    @Override
    public void mouseClicked(MouseEvent event) {
        RestConfig config = project.getComponent(RestServicesNavigator.class).getConfig();
        int x = event.getX();
        int y = event.getY() + 18;
        JPopupMenu menu = new JPopupMenu();
        JMenuItem item = new JMenuItem(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.setting.environment.new"));//创建菜单项(点击菜单项相当于点击一个按钮)
        item.setIcon(Consts.Icons.NEW);
        item.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        item.addActionListener(ent -> {
            JMenuItem newItem = (JMenuItem) ent.getSource();
            newItem.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            new SettingListener(new SettingForm(project), new SettingListenerContext(SettingFormType.ENVIRONMENT, I18Bundle.getMessage("com.github.aloxc.plugin.restplus.setting.environment.new")))
                    .actionPerformed(null);
        });
        menu.add(item);

        if(config != null && config.getEnv() != null && config.getEnv().size() > 0){
            Map<String, EnvironmentConfig> envMap = config.getEnv();
            List<String> envKeyList = envMap.keySet().stream().sorted().collect(Collectors.toList());
            int index = 0;
            for(String envName : envKeyList){
                EnvironmentConfig envConfig = envMap.get(envName);
                String name = envConfig.getName();
                StringBuffer showName = new StringBuffer(name) ;
                String context = envConfig.getContext();
                if (!StringUtils.isNullOrEmpty(context)) {
                    if (context.startsWith("/")) {
                        context = context.substring(1);
                    }
                    if (context.endsWith("/")) {
                        context = context.substring(0, context.length() - 1);
                    }
                }else{
                    context = "";
                }
                showName.append(SPLIT)
                        .append(envConfig.isHttps() ? "https://" : "http://")
                        .append(envConfig.getHost())
                        .append(":")
                        .append(envConfig.getPort())
                        .append("/")
                        .append(context);
                //另存为
                item = new JMenuItem(showName.toString());
                item.setIcon(IconLoader.getIcon("/icons/num/" + (index + 1) + ".png"));
                index++;
                if(envConfig.isSelected()) {
                    item.setIcon(Consts.Icons.CUR);
                    item.setForeground(Consts.MAIN_COLOR);
                    item.setBackground(Consts.COOKIE_BACKGROUND_COLOR);
                }
                item.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                item.addActionListener(new SelectEnvironmentListener(project));
                menu.add(item);
            }
            menu.show((JButton)event.getSource(),x,y);
        }
    }
}

